package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.MyWechatQuestionAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.constant.UserInfoGlobal;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.WechatQuestionDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyWechatActivity extends BaseActivity {


    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.list_one)
    RecyclerView listOne;
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.btn_wechat_bottom)
    LinearLayout btnWechatBottom;
    private Unbinder unbiner;
    private MyWechatQuestionAdapter adapter;
    private WechatQuestionDialog questionDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wechat);
        initBar();
        setBarClick();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtName.setText("HI,"+ UserInfoGlobal.getUserNick());
        questionDialog = new WechatQuestionDialog(this);
        questionDialog.setOnSelect(new WechatQuestionDialog.OnSureListener() {
            @Override
            public void onUse(String id) {

            }

            @Override
            public void onNotUse(String id) {

            }
        });
        adapter = new MyWechatQuestionAdapter(this);
        adapter.setOnChooseListener(new MyWechatQuestionAdapter.OnChooseListener() {
            @Override
            public void onSuccess(String id, String title) {
                questionDialog.setId(id, title);
                questionDialog.show();
            }
        });
        listOne.setAdapter(adapter);
        getXueXinQuestionList();

    }

    public void getXueXinQuestionList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();

        Api.api_service.getXueXinQuestionList().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.update(result.data);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_wechat, R.id.btn_wechat_bottom})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_wechat:
            case R.id.btn_wechat_bottom:
                Util.startActivity(this, WechatChatActivity.class);
                break;
        }
    }
}
